package com.eboss2.sbs.weight;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.EditText;

import com.eboss2.sbs.activity.ActivityChat;


/**
 * 自定义的textview，用来处理复制粘贴的消息
 * 
 * @author morton
 * 
 */
public class PasteEditText extends EditText {

	private Context context;

	public PasteEditText(Context context) {
		super(context);
		this.context = context;
	}

	public PasteEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public PasteEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	public boolean onTextContextMenuItem(int id) {
		if (id == android.R.id.paste) {
			ClipboardManager clip = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
			String text = clip.getText().toString();
			if (text.startsWith(ActivityChat.COPY_IMAGE)) {
				text = text.replace(ActivityChat.COPY_IMAGE, "");
				Intent intent = new Intent(context, AlertDialog.class);
				intent.putExtra("title", "发送以下图片？");
				intent.putExtra("forwardImage", text);
				intent.putExtra("cancel", true);
				((Activity) context).startActivityForResult(intent, ActivityChat.REQUEST_CODE_COPY_AND_PASTE);
			}
		}
		return super.onTextContextMenuItem(id);
	}

	@Override
	protected void onTextChanged(CharSequence text, int start,
			int lengthBefore, int lengthAfter) {
		if (!TextUtils.isEmpty(text) && text.toString().startsWith(ActivityChat.COPY_IMAGE)) {
			setText("");
		}
		super.onTextChanged(text, start, lengthBefore, lengthAfter);
	}

}
